package com.service1.db.redis

import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit

@Service
class RedisService(val redisTemplate: RedisTemplate<String, String>) {
    fun saveID(value: String, timeout: Long? = null) {
        if (timeout == null)
            redisTemplate.opsForValue().set("id", value)
        else
            redisTemplate.opsForValue().set("id", value, timeout, TimeUnit.SECONDS)
    }

    fun readID(): String {
        return redisTemplate.opsForValue().get("id") ?: ""
    }

    fun readSomething(): String {
        val key = "id"
        val cacheResult = redisTemplate.opsForValue().get(key)
        return if (cacheResult != null) cacheResult
        else {
            val value = "此处查库操作"
            redisTemplate.opsForValue().set(key, value, 60, TimeUnit.SECONDS)
            value
        }
    }
}
